<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\CaseDesignerService;
use Illuminate\Http\Request;

class CaseDesignerController extends BaseController
{
    //设计师

    public function __construct(CaseDesignerService $service)
    {
        $this->db = $service;
    }

    //选项
    public function getOption(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res);
    }

    //列表
    public function getIndex(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = $this->db->getIndex($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function getOne(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->post('id', 0);
        $pas['realname'] = $request->post('realname', '');
        $pas['head'] = $request->post('img', '');
        $pas['cate_id'] = $request->post('cateid', '');
        $pas['work_year'] = $request->post('work_year', '');
        $cname = $request->post('cname', '');
        if (!$pas['realname']) {
            return $this->toJson(-2, '姓名必须的');
        }
        if ($cname) {
            $pas['cid'] = $this->db->cidByCname($cname);
            if (!$pas['cid']) {
                return $this->toJson(-2, '所属公司不存在');
            }
        } else {
            $pas['cid'] = 0;
        }
        if ($id) {
            $res = $this->db->modify($id, $pas);
        } else {
            $res = $this->db->add($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->post('id', 0);
        $res = $this->db->del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}